var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      hot_list:[],
      content:'',
      showView:false,
      types:'keyword',
      searchKey:'',
      history:'',
      nocancel:false,
      search_show:false
  },
  onLoad: function () {
    
    var that = this
    var history = wx.getStorageSync('history_info')

    wx.request({
      url: app.configData.shop.api_url +'/index.php?ctl=Index&met=getSearchKeyList&typ=json',
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          hot_list: res.data.data.list,
          searchKey:res.data.data.list,
        })
      }
    })

    if(history){
      that.setData({
        history: history.split(",")
      })
    }

  },
  //获取用户输入的用户名
  contentInput: function (e) {
    if (e.detail.value){
      this.setData({
        content: e.detail.value,
        search_show: true
      })
    }else{
      this.setData({
        search_show: false
      })
    }
    
  },
   //事件处理函数
  bindViewTap: function (e) {
    var that = this;
    var urls = '';
    var content = that.data.content
    if (content != ''){
      
      that.add_keyword(content)
    }
    
    if(that.data.types == 'keyword'){
      urls = '../search_list/search_list?content=' + content
    }else{
      urls = '../store_list/store_list?content=' + content
    }
    wx.redirectTo({
      url: urls
    })
 
  },
  
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  //选择商品搜索，默认商品搜索
  goods:function(){
    var that = this;
    that.setData({
      types: 'keyword',
      showView: (!that.data.showView)
    })
  },
  //点击历史搜索
  jup:function(e){
    var that = this 
    var urls = '';
    var content = e.currentTarget.dataset.keywords
    if (that.data.types == 'keyword') {
      urls = '../search_list/search_list?content=' + content
    } else {
      urls = '../store_list/store_list?content=' + content
    }
    wx.redirectTo({
      url: urls
    })
  },
  //点击热门搜索
  hot_jup: function (e) {
    var that = this
    var urls = '';
    var content = e.currentTarget.dataset.keywords
    if (that.data.types == 'keyword') {
      urls = '../search_list/search_list?content=' + content
    } else {
      urls = '../store_list/store_list?content=' + content
    }
    wx.redirectTo({
      url: urls
    })
  },
  //选择店铺搜索
  shop:function(){
    var that = this;
    that.setData({
      types: 'shop',
      showView: (!that.data.showView)
    })
    
  },
  //添加历史搜索记录
  add_keyword:function(content){
    var that = this
    var history = []
    var info = wx.getStorageSync('history_info')
    var hist = info.split(",")
    if (hist.length > 0){
      for (var i = 0; i < hist.length;i++){
        if (hist[i] != ''){
          history.push(hist[i])
        }
        if (hist[i] == content){
          return false
        }
      }
    }
    
    if (hist.length > 10){
     
      history.shift()  
    }
    history.push(content)
    that.setData({
      history: history
    })
   
    wx.setStorage({
      key: 'history_info',
      data: history.toString()
    })
   
  },
  //删除历史搜索记录
  delete_history:function(){
    this.setData({
      nocancel:true 
    })
  },
  confirm:function(){
    wx.removeStorageSync('history_info')
    this.setData({
      history: '',
      nocancel: false,
    })
  },
  cancel: function () {
    this.setData({
      nocancel: false,
    })
  }, 
  //删除搜索框内容
  search_clear:function(){
    var that = this
    that.setData({
      content:'',
      search_show:false
    })
  }

})